package com.cpi.cornpp.poplanned.reals.services;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.poplanned.reals.converter.PoPlannedRealsConverter;
import com.cpi.cornpp.poplanned.reals.dao.PoPlannedRealsDAO;
import com.cpi.cornpp.poplanned.reals.dto.PoPlannedRealsDTO;
import com.cpi.cornpp.poplanned.reals.exception.PoPlannedRealsException;
import com.cpi.cornpp.poplanned.reals.view.PoPlannedRealsView;

/**
 * Po Planned Reals Service
 * @author Edgar Joao
 *
 */
public class PoPlannedRealsService {

	private static final String NUMBER_VALUE = "number";


	/**
	 * Add a new PoPlanned Reals
	 * @param view
	 */
	public void addPoPlannedReals(PoPlannedRealsView view) throws PoPlannedRealsException{
		PoPlannedRealsDAO poPlannedRealsDAO = null;
		PoPlannedRealsConverter poPlannedRealsConverter = null;		
		PoPlannedRealsDTO poPlannedRealsDTO = null;
		boolean result = false;
		try {
			
			poPlannedRealsDAO = new PoPlannedRealsDAO();
			poPlannedRealsConverter = new PoPlannedRealsConverter();
			poPlannedRealsDTO = new PoPlannedRealsDTO();
			
			poPlannedRealsDTO = poPlannedRealsConverter.converterPoPlannedRealsViewToDTO(view);
			
			result = poPlannedRealsDAO.existPlannedReals(poPlannedRealsDTO);
			if(view.getPoNumber().equalsIgnoreCase(NUMBER_VALUE)){				
				poPlannedRealsDAO.deletePlannedReals(poPlannedRealsDTO);
			}else if(!result){
				poPlannedRealsDAO.addPlannedReals(poPlannedRealsDTO);
			}else if(result){
				poPlannedRealsDAO.updatePlannedReals(poPlannedRealsDTO);
			}
			
		} catch (PoPlannedRealsException poPlannedRealsException) {
			throw poPlannedRealsException;
		}
		catch (Exception e){
			PoPlannedRealsException poPlannedRealsException=null;
			e.printStackTrace(System.out);
			poPlannedRealsException = new PoPlannedRealsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw poPlannedRealsException;
		}
	}
	
	
	/**
	 * 
	 * Get a PoPlanned Reals By Id
	 * 
	 * @param view
	 * @return
	 * @throws PoPlannedRealsException
	 */
	public PoPlannedRealsView getPoPlannedRealsById(PoPlannedRealsView view) throws PoPlannedRealsException{
		
		PoPlannedRealsDAO poPlannedRealsDAO = null;
		PoPlannedRealsConverter poPlannedRealsConverter = null;		
		PoPlannedRealsDTO poPlannedRealsDTO = null;
		PoPlannedRealsView poPlannedRealsView = null;
		
		try {
			
			poPlannedRealsDAO = new PoPlannedRealsDAO();
			poPlannedRealsConverter = new PoPlannedRealsConverter();
			poPlannedRealsDTO = new PoPlannedRealsDTO();
			poPlannedRealsView = new PoPlannedRealsView();
			
			poPlannedRealsDTO = poPlannedRealsConverter.converterPoPlannedRealsViewToDTO(view);
			
			poPlannedRealsView = poPlannedRealsConverter.converterPoPlannedRealsDtoToView(poPlannedRealsDAO.getPoPlannedRealsById(poPlannedRealsDTO));
			
		} catch (PoPlannedRealsException poPlannedRealsException) {			
			poPlannedRealsException.printStackTrace(System.out);
			poPlannedRealsException= new PoPlannedRealsException(poPlannedRealsException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw poPlannedRealsException;
		}
		catch (Exception e){
			PoPlannedRealsException poPlannedRealsException=null;
			e.printStackTrace(System.out);
			poPlannedRealsException = new PoPlannedRealsException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw poPlannedRealsException;
		}
		return poPlannedRealsView;
	}
}
